﻿@using Website.CRUDServices
@using Location = Website.Models.Domain.Location
@using Profile = Website.CRUDServices.Profile
@using Region = Website.Models.Domain.Region
@{
    this.ViewBag.Title = "Opdatér Klynge";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var Regions = this.ViewData["Regions"] as List<Region>;
    var Locations = this.ViewData["Locations"] as List<Location>;
    var Profiles = this.ViewData["Profiles"] as List<Profile>;
    var ClusterStatusTypes = this.ViewData["ClusterStatusTypes"] as List<ClusterStatusType>;
    var ClusterMethodTypes = this.ViewData["ClusterMethodTypes"] as List<ClusterMethodType>;
    var Projects = this.ViewData["Projects"] as List<Project>;
    var errorMsg = new HtmlString(string.Empty);
    int counter = 0;
    var Cluster = Model as Cluster;
    var ClusterMethods = this.ViewData["ClusterMethods"] as List<ClusterMethod>;
    var ClusterStatuses = this.ViewData["ClusterStatuses"] as List<ClusterStatus>;
    var clusterStatusTypes = ClusterStatuses.Select(x => x.Type).ToList();
    var clusterMethodTypes = ClusterMethods.Select(x => x.Type).ToList();
    var filesInfo = this.ViewData["FilesInfo"] as List<Website.CRUDServices.File> ?? new List<Website.CRUDServices.File>();
    if (this.ViewData["ErrorMessage"] != null)
    {
        errorMsg = this.ViewData["ErrorMessage"] as HtmlString;
    }

}

@Html.Partial("~/Views/_Shared/LogView.cshtml", Cluster.ID)

<script>
    //datepicker script
    $(function () {
        $("#datepicker").datepicker();
        $('#OwnerProjects').multiSelect();
    });
</script>

<script type="text/javascript">
    //toggle script toggles div with id = id
    function toggle(id) {
        if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == "") {
            document.getElementById(id).style.display = 'block';
        } else {
            document.getElementById(id).style.display = 'none';
        }
    }
</script>

@using (this.Html.BeginForm("UpdateCluster", "Cluster", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="error">
        @errorMsg
    </div>
    // *******************Cluster*************************
    // hidden field to hold ID of the object
    <input type="hidden" id="ID" name="ID"  readonly="readonly" value="@Cluster.ID" />
    // name
    <div class="formBullet">Navn på klynge</div>
    <input type="text" id="Name" name="Name" value="@Cluster.Name"/>
    
                // date
    <div class="formBullet">Dato</div>
    <input type="text" id="datepicker" name="Date" readonly="readonly" value="@Cluster.Date" />

    // location
    <div class="formBullet">Lokation</div>
    <div class="formEntry">
        <select name="Location" id="Location">
            <option value="@Guid.Empty">Vælg en lokation</option>
            @foreach (Location loc in Locations.OrderBy(x => x.Name))
            {
                if (loc.ID == Cluster.Location)
                {
                <option value="@loc.ID" selected="selected">@loc.Name</option>
                }
                else
                {
                <option value="@loc.ID">@loc.Name</option>
                }
            }
        </select>
    </div>
    
            // national or international?
    <div class="formBullet">Hvad er projektet?</div>
    <div>
        <input type="radio" name="IsNational" value="National"@if (Cluster.IsNational)
                                                              {@Html.Raw("checked")}>Nationalt
        <input type="radio" name="IsNational" value="International"@if (!Cluster.IsNational)
                                                                   {@Html.Raw("checked")}>Internationalt
    </div>

                                                                   // ownerProjects chooser
    <div class="formBullet">Alle projekter tilknyttet</div>
    <div class="formEntry">
        <select multiple="multiple" name="OwnerProjects" id="OwnerProjects">
            @foreach (Project proj in Projects.Where(x => (Cluster.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID" selected="selected">@proj.Name</option>
            }
            @foreach (Project proj in Projects.Where(x => !(Cluster.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID">@proj.Name</option>
            }
        </select>
    </div>

            // profiles chooser
    <div class="formBullet">Profiler</div>
    <div class="formEntry">
        <select multiple="multiple" name="Profiles" id="Profiles">
            @foreach (Profile prof in Profiles.Where(x => (Cluster.Profiles ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID" selected="selected">@prof.Name</option>
            }
            @foreach (Profile prof in Profiles.Where(x => !(Cluster.Profiles ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID">@prof.Name</option>
            }
        </select>
        <script src="@Url.Content("jquery.multi-select.js")" type="text/javascript"> </script>
    </div>

            // *******************ClusterStatus*************************
    <div class="formBullet">Status</div>

    <br />
            foreach (ClusterStatusType cst in ClusterStatusTypes)
            {
    <hr />
<fieldset class="chosenFields">
        <legend>
        <input type="checkbox" onclick="@Html.Raw("toggle('" + cst.ID + "');")" name="ClusterStatus[@counter].Checked" value="true" @if (clusterStatusTypes.Contains(cst.ID))
                                                                                                                                    {@Html.Raw("checked")}/>@cst.Name<br />
    </legend>
                                                                                                                                    @if (clusterStatusTypes.Contains(cst.ID))
                                                                                                                                    {@Html.Raw("<script>$(function () {toggle('" + cst.ID + "');});</script>")}
    <div class="formEntry open_hidden" id="@cst.ID">
        <!--guid-->
        <input type="hidden" name="ClusterStatus[@counter].Type" value="@cst.ID" />
        @if (ClusterStatuses.Any(x => x.Type == cst.ID))
        {
            <input type="hidden" name="ClusterStatus[@counter].ID" value="@ClusterStatuses.Single(x => x.Type == cst.ID).ID" />
            
        }
        <!--active?-->
        <div class="formEntry">
            <input type="radio" name="ClusterStatus[@counter].IsActive" value="true"@if (ClusterStatuses.Where(x => x.Type == cst.ID).Any() && ClusterStatuses.Single(x => x.Type == cst.ID).IsActive)
                                                                                    {@Html.Raw("checked")}>Aktiv
                <input type="radio" name="ClusterStatus[@counter].IsActive" value="false" @if (ClusterStatuses.Where(x => x.Type == cst.ID).Any() && !ClusterStatuses.Single(x => x.Type == cst.ID).IsActive)
                                                                                          {@Html.Raw("checked")}>Inaktiv
        </div>
        <!--comments-->
        <div class="formBullet">
            Kommentarer  til @cst.Name<br />
        </div>
        <div class="formEntry">
            <textarea name="ClusterStatus[@counter].Comments">@if (ClusterStatuses.Any(x => x.Type == cst.ID))
                                                              {
                                        @ClusterStatuses.Single(x => x.Type == cst.ID).Comments
                                                              }</textarea>
        </div>
    </div>
    </fieldset>
                                                              counter++;
            }

            counter = 0;

            // *******************ClusterMethod*************************
    <div class="formBullet">Metode</div>
        <br />
    
            foreach (ClusterMethodType cmt in ClusterMethodTypes)
            {
    <fieldset class="chosenFields">
        <legend>
        <input type="checkbox" onclick="@Html.Raw("toggle('" + cmt.ID + "');")" name="ClusterMethod[@counter].Checked" value="true" @if (clusterMethodTypes.Contains(cmt.ID))
                                                                                                                                    {@Html.Raw("checked")}/>@cmt.Name<br />
    </legend>
                                                                                                                                    @if (clusterMethodTypes.Contains(cmt.ID))
                                                                                                                                    {@Html.Raw("<script>$(function () {toggle('" + cmt.ID + "');});</script>")}
    <div class="formEntry open_hidden" id="@cmt.ID">
        <!--guid-->
        <input type="hidden" name="ClusterMethod[@counter].Type" value="@cmt.ID" />
        @if (ClusterMethods.Any(x => x.Type == cmt.ID))
        {
            <input type="hidden" name="ClusterMethod[@counter].ID" value="@ClusterMethods.Single(x => x.Type == cmt.ID).ID" />
        }
        <!--comments-->
        <div class="formBullet">
            Kommentarer til @cmt.Name<br />
        </div>
        <div class="formEntry">
            <textarea name="ClusterMethod[@counter].Comments">@if (ClusterMethods.Any(x => x.Type == cmt.ID))
                                                              {
                                        @ClusterMethods.Single(x => x.Type == cmt.ID).Comments
                                                              }</textarea>
        </div>
        <!--filechooser-->
        <div class="formBullet">
            Fil til arkivering<br />
        </div>
        <div class="formEntry">
            @FileUpload.GetHtml("ClusterMethod[" + counter + "].PostedFiles", 0, true, false, "Tilføj flere filer", "Vælg fil til arkivering")
        </div>

        <div class="formBullet">
            Eksisterende filer<br />
        </div>
        <div class="formEntry">
            <script type="text/javascript">
                    function openFile(id)
                    {
                        var url = "@Url.Action("Open", "File", new { ID = ".id." })";
                        window.open(url.replace(".id.", id), "blank_" + id);
                    }
            </script>
            <table class="fileTable" id="FileTable">
            @if (ClusterMethods.Any(x => x.Type == cmt.ID))
            {
                var fileIDs = ClusterMethods.Single(x => x.Type == cmt.ID).Files;

                foreach (var file in filesInfo.Where(x => fileIDs.Contains(x.ID)))
                {
                    <tr id="@Html.Raw("tr_" + file.ID)">
                        <td>@file.Name</td>
                        <td>
                            <input type="button" value="Se fil" onclick="@Html.Raw("javascript:openFile('" + file.ID + "');")" />
                        </td>
                        <td><input type="button" value="Slet fil" class="deleteButton" id="@Html.Raw("del_" + file.ID)"/>
                            <script>
                                $("#@Html.Raw("del_" + file.ID)").click(function () {
                                    $('#OwnerClusterMethodID').val("@ClusterMethods.Single(x => x.Type == cmt.ID).ID");
                                    $('#ClusterMethodFileID').val("@file.ID.ToString()");
                                    $("#deleteDialog").dialog({
                                        width: "340px",
                                        modal: true
                                    });
                                });
                            </script>

                        </td> 
                    </tr>
                }
            }
            </table>
        </div>

        <!--arkchive location-->
        <div class="formBullet">Fysisk arkiverings placering</div>
        <input type="text" id="Archive" name="ClusterMethod[@counter].ArchiveLocation" value="@if (ClusterMethods.Any(x => x.Type == cmt.ID))
                                                                                              {
                                        @ClusterMethods.Single(x => x.Type == cmt.ID).ArchiveLocation
                                                                                              }" />
    </div>
    </fieldset>
                                                                                              counter++;
            }

    <div class="buttonHolder">
        <script type="text/javascript">
            function confirmDelete() {
                if (confirm("Er du sikker på at du vil slette aktiviteten? Dette valg kan ikke fortrydes.")) {
                    location.href = '@this.Url.Action("DeleteCluster", new { ID = Cluster.ID })';
                }
            }
        </script>
            <input type="button" value="Slet" class="deleteButton margin-right-20" 
                 onclick="confirmDelete();"
                />
        <input type="submit" value="Opdatér" />
    </div>
}

<script>

     $(function () {
        $('#Profiles').multiSelect();
       

        $('#DeleteClusterMethodFileForm').submit(function (e) {
            e.preventDefault();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    if (result.success) {
                        //var methodTypeID = $('#ClusterMethodTypeID').val();
                        var fileID = $('#ClusterMethodFileID').val();

                        $('#tr_' + fileID).remove();


                        $("#deleteDialog").dialog('close');
                    } else {
                        // We have the partial with errors in case of failure
                        // so all we have to do is update the DOM
                        $('#DeleteClusterMethodFileForm').html(result);
                    }
                }
            });
        });
    });
</script>

<div id="deleteDialog" title="Slet fil?" style="display: none;">
@using (this.Html.BeginForm("DeleteClusterMethodFileJson", "Cluster", FormMethod.Post, new { id = "DeleteClusterMethodFileForm" }))
{
    <input type="hidden" name="OwnerClusterMethodID" id="OwnerClusterMethodID" value="" />
    <input type="hidden" name="ClusterMethodFileID" id="ClusterMethodFileID" value="" />@*
    <input type="hidden" name="ClusterMethodTypeID" id="ClusterMethodTypeID" value="" />*@
    <p>
        Er du sikker på at du vil slette filen?<br />
        (Denne handling kan ikke fortrydes)
    </p>
    
    <div class="buttonHolder">
        <input type="submit" value="Slet" />
    </div>
}
</div>